package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.PraiseDao;
import com.zretc.support.entity.Praise;
import com.zretc.support.service.PraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PraiseServiceImpl implements PraiseService {

    @Autowired
    private PraiseDao praiseDao;

    @Override
    public int addPraise(int replyId, String userId) {
        Praise praise = new Praise();
        praise.setReplyId(replyId);
        praise.setUserId(userId);
        praise = praiseDao.save(praise);
        if (praise!=null){
            return 1;
        }
        return 0;
    }

    @Override
    public int cancelPraise(int replyId, String userId) {
        Praise praise = praiseDao.findPraiseByReplyIdAndUserId(replyId,userId);
        try{
            praiseDao.deleteById(praise.getPraiseId());
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int selPraiseNum(int replyId) {

        List<Praise> praisesList = praiseDao.findPraiseByReplyId(replyId);
        return praisesList.size();
    }
}
